<template>
  <div>
    <input type="file" @change="handleFileChange" />
    <button @click="handleUpload">上传</button>
    <div class="process-wrapper">
      <div>总进度：</div>
      <div class="process">
        <div
          class="completed"
          :style="{
            width: 4 * uploadPercentage + 'px',
          }"
        >
          <span style="padding-right: 10px">{{ uploadPercentage }}%</span>
        </div>
      </div>
    </div>
    <div class="chunk-item-process">
      <div class="title">切片进度</div>
      <div class="item" v-for="item in data" :key="item">
        <div class="process-wrapper">
          <div>切片{{ item.index }}：</div>
          <div class="process">
            <div
              class="completed"
              :style="{
                width: 4 * item.percentage + 'px',
              }"
            >
              <span style="padding-right: 10px">{{ item.percentage }}%</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, ref, computed } from "@vue/reactivity";

import {
  uploadChunks,
  verifyUpload,
  structureChunkData,
} from "../hook/useUpload";
import { calculateHash } from "../hook/useCalculateHash";
import { useUploadPercentage } from "../hook/useProcess";
import { createFileChunk } from "../hook/useFileHandler";
// import { CHUNK_SIZE, PORT } from "../../../const";
// const {CHUNK_SIZE, PORT} = require('../../../const');
const CHUNK_SIZE = 1024 * 1024 * 10;

export default {
  setup(props) {
    const container = reactive({ file: null });
    const dataRef = ref([]);
    const hashPercentageRef = ref("");

    // 处理上传
    const handleUpload = async () => {
      if (!container.file) return;

      //根据container.file生成切片数组
      const fileChunkList = createFileChunk(container.file, CHUNK_SIZE);

      // 计算文件hash
      container.hash = await calculateHash(hashPercentageRef, fileChunkList);

      // 校验文件是否存在于服务端
      const { shouldUpload } = await verifyUpload(
        container.file.name,
        container.hash
      );

      if (!shouldUpload) {
        return alert("服务端已存在这个文件");
      }

      // 构建要上传的切片数组数据结构
      dataRef.value = structureChunkData(fileChunkList, container);

      // 上传切片
      await uploadChunks(dataRef.value, container);
    };

    // 保存要上传的文件
    const handleFileChange = (e) => {
      const [file] = e.target.files;
      /**
       * name: '',
       * size: 180196 / 175k;
       */
      if (!file) return;
      container.file = file;
    };

    // 计算总进度（百分之多少）
    const uploadPercentage = useUploadPercentage(dataRef, container);

    return {
      container,
      data: dataRef,
      handleUpload,
      handleFileChange,
      uploadPercentage,
    };
  },
};
</script>

<style scoped>
.process {
  background-color: grey;
  width: 400px;
  border-radius: 4px;
  color: #fff;
  position: relative;
}
.process .completed {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  border-radius: 4px;
  background-color: #f90;
  transition: width 0.3s linear;
}
.process .completed .text {
  padding-right: 20px;
  display: inline-block;
}
.process-wrapper {
  display: flex;
  width: 500px;
  text-align: right;
  justify-content: space-around;
}
.chunk-item-process {
  margin-top: 30px;
}
.chunk-item-process .title {
  font-size: 20px;
  font-weight: bold;
}
.chunk-item-process .item {
  margin-top: 10px;
}
</style>
